import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class work1 {
    public static List<Integer> findSubstring(String s, String[] words) {
        List<Integer> list=new ArrayList<>();
        HashMap<String,Integer>map1=new HashMap<>();
        for (String w:words) {
            map1.put(w,map1.getOrDefault(w,0)+1);
        }
        int temp=0;
        while (temp< words[0].length()){
            int right=temp;
            int left=temp;
            int count=0;
            HashMap<String,Integer>map=new HashMap<>();//**这个也需要刷新
            while (right+words[0].length()<=s.length()){
                String tr =s.substring(right,right+words[0].length());
                map.put(tr,map.getOrDefault(tr,0)+1);
                if(map.get(tr)<=map1.getOrDefault(tr,0))count++;
                if(right-left+1>words.length*words[0].length()){
                    String tl=s.substring(left,left+words[0].length());
                    if(map.get(tl)<=map1.getOrDefault(tl,0))count--;
                    map.put(tl,map.getOrDefault(tl,0)-1);
                    left+=words[0].length();
                }
                if(count==words.length)list.add(left);
                right+=words[0].length();
            }
            temp++;
        }
        return list;
    }

    public static void main(String[] args) {
        String s="barfoofoobarthefoobarman";
        String []worid={"bar","foo","the"};
        List<Integer>list=findSubstring(s,worid);
        System.out.println(list.toString());
    }
}
